<template>
	<div class="add-jq-dialog">
		
		<appdialog 
			ref="dialog"
      title="新增部门"
			:visible="false" 
			width="25%"
			@confirm="confirmAdd"
			@cancle="cancleAdd"
			>
      <el-form  label-width="100px" :model="ruleForm" ref="ruleForm" :rules="rules">
        <el-form-item label="部门名称：" prop="deptName">
          <el-input v-model="ruleForm.deptName" style="width:155px;"></el-input>
        </el-form-item>
				<!-- <el-form-item label="部门状态：" prop="status">
          <el-select v-model="ruleForm.status">
						<el-option value="1" label="启用"></el-option>
						<el-option value="2" label="禁用"></el-option>
					</el-select>
        </el-form-item> -->
      </el-form>
		</appdialog>
	</div>
</template>

<script>
import {saveDept} from '@/service/swagger/DeptController.js'
import appdialog from "@/view/components/dialog/dialog";
export default {
	name: "addjs",
	components: {
		appdialog,
	},
	data() {
		return {
			ruleForm:{
				deptName:'',
				parentDeptId:'0',
				status:'1',
			},
			rules:{
				deptName:[
				  { required: true, message: '请输入部门名称', trigger: 'blur' },
				],
				// status:[
				// 	{ required: true, message: '请输选择部门状态', trigger: 'change' },
				// ]
			}
		}
	},
	mounted() {

	},
	methods: {
		open(row) {
			console.log(row)
			if(row){
				this.ruleForm = {
					deptName:'',
					parentDeptId:row.id,
				}
			}
			this.$refs.dialog.open();
		},
		confirmAdd() {
			this.$refs['ruleForm'].validate((valid) => {
				if (valid) {
					this.submit()
				} else {
					setTimeout(() => {
						this.open();
					}, 50)
					return false;
				}
			})
		},
		submit(){
      saveDept(this.ruleForm).then(ret=>{
				if(ret&&ret.code == 0){
					this.$message({message: '添加部门成功',type: 'success'})
					this.$emit('handleRefsh')
					this.cancleAdd()
				}else{
					this.cancleAdd()
				}
			})
		},
		cancleAdd() {
			  this.$refs.ruleForm.clearValidate(['deptName'])
				this.ruleForm = {
					deptName:'',
					parentDeptId:'0',
				}
		},
	}
}
</script>

<style scoped lang="scss">
@import "../../../../styles/variables.scss";
</style>